package com.code.petapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.code.petapp.R;
import com.code.petapp.bean.FosterBean;

import java.util.List;

public class FosterImgAdapter extends RecyclerView.Adapter<FosterImgAdapter.MyViewHolder> {
    private Context context;
    private List<FosterBean> list;
    private View inflater;
    private OnItemClickListener mListener;

    public FosterImgAdapter(Context context, List<FosterBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(context).inflate(R.layout.foster_img_item, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(inflater);
        return myViewHolder;
    }
    public interface OnItemClickListener {
        void editOnClick(View parent, int position);
        void endOnClick(View parent, int position);
        void cancelOnClick(View parent, int position);
    }
    // 设置点击事件
    public void setOnItemClickListener(OnItemClickListener l) {
        this.mListener = l;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        FosterBean fosterBean = list.get(position);
        holder.room.setText(fosterBean.getRoomName());
        holder.name.setText(fosterBean.getName());
        holder.time.setText(fosterBean.getTime());
        holder.day.setText(fosterBean.getDays());
        holder.status.setText(fosterBean.getStatus());
        if(fosterBean.getStatus().equals("寄养中")){
            holder.house.setImageResource(R.drawable.icons_house1);
            holder.btnEnd.setVisibility(View.VISIBLE);
            holder.edit.setVisibility(View.GONE);
            holder.cancel.setVisibility(View.GONE);
        }else if(fosterBean.getStatus().equals("待审核")){
            holder.house.setImageResource(R.drawable.icons_wait);
            holder.btnEnd.setVisibility(View.GONE);
            holder.edit.setVisibility(View.VISIBLE);
            holder.cancel.setVisibility(View.VISIBLE);

        }else{
            holder.house.setImageResource(R.drawable.icons_house);
            holder.btnEnd.setVisibility(View.VISIBLE);
            holder.edit.setVisibility(View.GONE);
            holder.cancel.setVisibility(View.GONE);
            holder.btnEnd.setEnabled(false);
        }
        if(null != mListener) {
            holder.btnEnd.setOnClickListener(v -> mListener.endOnClick(holder.btnEnd, position));
            holder.edit.setOnClickListener(v -> mListener.editOnClick(holder.edit, position));
            holder.cancel.setOnClickListener(v -> mListener.cancelOnClick(holder.cancel, position));
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView room;
        TextView name;
        TextView time;
        TextView day;
        TextView status;
        Button btnEnd;
        Button edit;
        Button cancel;
        ImageView house;

        public MyViewHolder(View itemView) {//这个view参数就是recyclerview子项的最外层布局
            super(itemView);
            //可以通过findViewById方法获取布局中的TextView
            room = itemView.findViewById(R.id.room);
            name = itemView.findViewById(R.id.name);
            time = itemView.findViewById(R.id.time);
            day = itemView.findViewById(R.id.day);
            status = itemView.findViewById(R.id.status);
            btnEnd= itemView.findViewById(R.id.btn_end);
            edit=itemView.findViewById(R.id.btn_edit);
            cancel=itemView.findViewById(R.id.btn_cancel);
            house= itemView.findViewById(R.id.house);
        }
    }
}
